package com.xzy.dao.impl;

import com.xzy.dao.IOrderItemDao;
import com.xzy.domain.OrderItem;
import com.xzy.utils.jdbcUtils;

import java.sql.Connection;
import java.util.List;

public class OrderItemDaoImpl extends BaseDao<OrderItem> implements IOrderItemDao {

    @Override
    public void addOrderItem(OrderItem orderItem) {
        Connection conn = jdbcUtils.getConnection();
        String sql="insert into t_order_item (id,name,count,price,total_price,order_id) value(?,?,?,?,?,?)";
        this.update(conn,sql,orderItem.getId(),orderItem.getName(),orderItem.getCount(),orderItem.getPrice(),
                orderItem.getTotal_price(),orderItem.getOrder_id());
        //此时不提交(此dao有事务),应该留到事务统一处理后提交
    }

    @Override
    public List<OrderItem> queryByOrderId(String OrderId) {
        Connection conn = jdbcUtils.getConnection();
        String sql="select * from t_order_item where order_id=?";
        List<OrderItem> orderItems = this.queryForList(conn, sql, OrderId);
        jdbcUtils.clossAndCommit();//因为获取连接时设置了手动提交所以必须调用该方法(提交并关闭)否则就会连接超时(数据库操作后一直不提交)
        return orderItems;
    }

    @Override
    public OrderItem queryByOrderItemId(Integer OrderItemId) {
        Connection conn = jdbcUtils.getConnection();
        String sql="select * from t_order_item where id=?";
        OrderItem orderItem = this.queryForOne(conn, sql, OrderItemId);
        jdbcUtils.clossAndCommit();//因为获取连接时设置了手动提交所以必须调用该方法(提交并关闭)否则就会连接超时(数据库操作后一直不提交)
        return orderItem;

    }
}
